$(function(){
    getArticleTypes();
    getLabels();
});


//请求文章类型列表
function getArticleTypes(){
    httpPost("education_api/article-type/query",function(result){

        for (let i = 0; i < result.length; i++) {
            var option = new Option();
            option.value = result[i].id;
            option.text = result[i].name;
            $("#types").append(option);
        }

    })
}

//请求用户标签列表
function getLabels(){
    let params = {userId:99};
    httpPostWithParams("education_api/label/selectLabelByUserId",params,function(result){
        for (let i = 0; i < result.length; i++) {
            var option = new Option();
            option.value = result[i].id;
            option.text = result[i].labelName;
            $("#labels").append(option);
        }
    })
}

//发布文章
function addArticle(){

    // 如果文章id存在，则请求修改文章接口
    if (articleId != null){
        editArticle();
        return;
    }

    let title = $("#title").val();
    if(title == ""){
        alert("标题不能为空")
        return;
    }

    let describe = $("#topicDescription").val();
    if(describe == ""){
        alert("文章描述不能为空")
        return;
    }

    let testEditormd = testEditor.getMarkdown();

    if(testEditormd == ""){
        alert("文章内容不能为空");
        return;
    }

    let articleTypeId = $("#types option:selected");


    let labelsArray = $("#labels option:selected");

    let labels = "";
    for (let i = 0; i < labelsArray.length; i++) {
        if(i != labelsArray.length - 1){
            labels += labelsArray[i].value + ",";
        }else{
            labels += labelsArray[i].value;
        }
    }


    console.log(title)
    console.log(testEditormd)
    console.log(articleTypeId.val())
    console.log(labels)

    let params = {
        title:title,
        content:testEditormd,
        articleDescribe:describe,
        articleTypeId:articleTypeId.val(),
        labelsStr:labels
    };

    httpPostWithParams("education_api/article/addArticle",params,function(result){
        alert("添加成功");
        location.href = "../article/articleList.html";
    });

    // $.post({
    //     url:baseUrl + "education_api/article/addArticle",
    //     // data:params,
    //     success:function(result){
    //         alert("添加成功")
    //     }
    // })
}

//返回上一个页面
function onBack(){
    window.history.go(-1); //返回上一页
}







